$(function(){
    obj.init();
});

var obj = new function () {
    'use strict';
    return {
        dom: {
            login: null,
            account: null,
            password: null,
            captcha: null,
            captchaImg: null,
            phone: null,
            sendCode: null,
            smsCode: null,
            remember: null,
            trust: null
        },
        params: {
            account: null,
            password: null,
            captcha: null,
            smsCode: null,
            remember: null
        },
        countdown: 60,
        trustAccount: null,
        init: function () {
            this.dom.login      = $('#bt_login');
            this.dom.account    = $('#account');
            this.dom.password   = $('#password');
            this.dom.captcha    = $('#captcha');
            this.dom.captchaImg = $('.captcha-img');
            this.dom.phone      = $('#phone');
            this.dom.sendCode   = $('#bt_send_code');
            this.dom.smsCode    = $('#smsCode');
            this.dom.remember   = $('#remember');
            this.dom.trust      = $('#trust');
            this.trustAccount   = this.dom.trust.attr('data-account');

            this.listener();
            this.funObj().checkBrowser();
        },
        listener: function () {
            var $this = this;
            //绑定回车
            document.onkeyup = function(e){
                var ev = document.all ? window.event : e;
                if(ev.keyCode === 13) {
                    $this.dom.login.click();
                }
            };
            //登陆
            $this.dom.login.click(function() {
                $this.funObj().login();
            });
            //点击验证码更新
            $this.dom.captchaImg.click(function () {
                 $(this).attr('src', $(this).attr('src'));
            });
            //发送验证码
            $this.dom.sendCode.click(function () {
                $this.funObj().sendCode();
            });
            //账号输入判断是否需要发送验证码
            $this.dom.account.blur(function () {
                $this.funObj().checkNeedValid();
            });
            //账号输入admin自动弹出手机验证
            // $this.dom.account.blur(function () {
            //     if ($(this).val() === 'admin') {
            //         $('.sms').slideDown();
            //         $this.dom.captcha.parent().hide();
            //     }
            //     else {
            //         $('.sms').slideUp();
            //         $this.dom.captcha.parent().slideDown();
            //     }
            // });
            //判断是否有记住密码cookie
            // $this.funObj().checkAutoLogin();
            //初始化记住密码及信任
            $this.funObj().initCheckBox();
            //渲染背景
            $this.funObj().parseBackground();
        },
        funObj: function(){
            var $this = this;
            return {
                login: function () {
                    $this.params.account  = $this.dom.account.val();
                    $this.params.password = $this.dom.password.val();
                    $this.params.captcha  = $this.dom.captcha.val();
                    $this.params.smsCode  = $this.dom.smsCode.val();
                    $this.params.remember = $this.dom.remember.prop('checked');
                    $this.params.trust    = $this.dom.trust.prop('checked');
                    if (!$this.params.account) {
                        $this.funObj().showError('用户名不能为空！');
                        return false;
                    }
                    // if (!$this.params.password) {
                    //     $this.funObj().showError('密码不能为空！');
                    //     return false;
                    // }
                    xhExtra.ajaxRequest('Index/loginIn', $this.params, {check: false}, function (result) {
                        if (result.error) {
                            $this.funObj().showError(result.error);
                            if (result.code === 401) {
                                $this.dom.captcha.parent().hide();
                                // $this.dom.password.parent().hide();
                                $('.sms').slideDown();
                                return false;
                            }
                            if (result.code === 402) {
                                // $this.dom.password.parent().slideDown();
                                $this.dom.captcha.parent().slideDown();
                            }
                            $this.dom.captchaImg.click();
                            return false;
                        }
                        window.location.reload();
                    });
                },
                //发送验证码
                sendCode: function () {
                    $this.dom.sendCode.attr('disabled', true);
                    xhExtra.ajaxRequest('index/getLoginSmsCode', {account: $this.dom.account.val(), phone: $this.dom.phone.val()}, {check: false}, function (result) {
                        if (result.error) {
                            $this.funObj().showError(result.error);
                            $this.dom.sendCode.attr('disabled', false);
                            return false;
                        }
                        $this.funObj().setCodeTime();
                    });
                },
                //发送验证码倒计时
                setCodeTime: function () {
                    if ($this.countdown === 0) {
                        $this.dom.sendCode.attr('disabled', false);
                        $this.dom.sendCode.text('发送验证码');
                        $this.countdown = 60;
                        return;
                    } else {
                        $this.dom.sendCode.attr('disabled', true);
                        $this.dom.sendCode.text("重新发送(" + $this.countdown + ")");
                        $this.countdown--;
                    }
                    setTimeout(function() {
                        $this.funObj().setCodeTime();
                    }, 1000)
                },
                checkNeedValid: function () {
                    var account = $this.dom.account.val();
                    xhExtra.ajaxRequest('Index/checkNeedValid', {account: account}, {check: false}, function (result) {
                        if (result.error) {
                            $('.sms').hide();
                            // $this.dom.password.parent().slideDown();
                            if (account === $this.trustAccount) {
                                $this.dom.captcha.parent().slideUp();
                            }
                            else {
                                $this.dom.captcha.parent().slideDown();
                            }
                            // if (account !== $this.rememberAccount) {
                            //     $this.dom.captcha.parent().slideDown();
                            // }
                            // else {
                            //     $this.dom.captcha.parent().hide();
                            // }
                            return false;
                        }
                        else {
                            $this.dom.captcha.parent().hide();
                            // $this.dom.password.parent().hide();
                            $('.sms').slideDown();
                            return false;
                        }
                    });
                },
                initCheckBox: function () {
                    if ($this.dom.trust.prop('checked')) {
                        $this.dom.captcha.parent().hide();
                    }
                    else if ($this.dom.account.val()) {
                        $this.dom.account.blur();
                    }
                },
                // checkAutoLogin: function () {
                //     if (document.cookie.match(new RegExp("(^| )autoLogin=([^;]*)(;|$)"))) {
                //         $this.rememberAccount = $this.dom.account.val();
                //         $this.dom.captcha.parent().hide();
                //         $this.dom.remember.prop('checked', true);
                //     }
                // },
                showError: function (msg) {
                    $.toast({
                        heading: '错误',
                        text: msg,
                        showHideTransition: 'slide',
                        position: 'bottom-center',
                        icon: 'error',
                        stack: false
                    })
                },
                parseBackground: function () {
                    particlesJS('particles', {
                        "particles": {
                            "number": {
                                "value": 80,
                                "density": {
                                    "enable": true,
                                    "value_area": 800
                                }
                            },
                            "color": {
                                "value": "#000"
                            },
                            "shape": {
                                "type": "circle",
                                "stroke": {
                                    "width": 0,
                                    "color": "#000000"
                                },
                                "polygon": {
                                    "nb_sides": 5
                                },
                                "image": {
                                    "width": 100,
                                    "height": 100
                                }
                            },
                            "opacity": {
                                "value": 0.5,
                                "random": false,
                                "aanim": {
                                    "enable": false,
                                    "speed": 10,
                                    "opacity_min": 0.1,
                                    "sync": false
                                }
                            },
                            "size": {
                                "value": 5,
                                "random": true,
                                "anim": {
                                    "enable": false,
                                    "speed": 40,
                                    "size_min": 0.1,
                                    "sync": false
                                }
                            },
                            "line_linked": {
                                "enable": true,
                                "distance": 150,
                                "color": "#000",
                                "opacity": 1,
                                "width": 1
                            },
                            "move": {
                                "enable": true,
                                "speed": 6,
                                "direction": "none",
                                "random": false,
                                "straight": false,
                                "out_mode": "out",
                                "attract": {
                                    "enable": false,
                                    "rotateX": 600,
                                    "rotateY": 1200
                                }
                            }
                        },
                        "interactivity": {
                            "detect_on": "canvas",
                            "events": {
                                "onhover": {
                                    "enable": true,
                                    "mode": "repulse"
                                },
                                "onclick": {
                                    "enable": true,
                                    "mode": "push"
                                },
                                "resize": true
                            },
                            "modes": {
                                "grab": {
                                    "distance": 400,
                                    "line_linked": {
                                        "opacity": 1
                                    }
                                },
                                "bubble": {
                                    "distance": 400,
                                    "size": 40,
                                    "duration": 2,
                                    "opacity": 8,
                                    "speed": 3
                                },
                                "repulse": {
                                    "distance": 200
                                },
                                "push": {
                                    "particles_nb": 4
                                },
                                "remove": {
                                    "particles_nb": 2
                                }
                            }
                        },
                        "retina_detect": true,
                        "config_demo": {
                            "hide_card": false,
                            "background_color": "#b61924",
                            "background_image": "",
                            "background_position": "50% 50%",
                            "background_repeat": "no-repeat",
                            "background_size": "cover"
                        }
                    });
                },
                checkBrowser: function () {
                    if (!$this.funObj().tryPassFail('let test;')) {
                        $.toast({
                            heading: '浏览器兼容错误！',
                            text: '为保证操作体验，请更换浏览器！',
                            showHideTransition: 'slide',
                            position: 'bottom-center',
                            icon: 'error',
                            allowToastClose: true,
                            hideAfter: false
                        });
                    }
                    // $('body').append('<script src="http://ruanyf.github.io/es-checker/es-checker.js"></script>');
                    // setTimeout(function(){
                    //     if (!Supports.letConst) {
                    //         $.toast({
                    //             heading: '浏览器兼容错误！',
                    //             text: '为保证操作体验，请更换浏览器！',
                    //             showHideTransition: 'slide',
                    //             position: 'bottom-center',
                    //             icon: 'error',
                    //             allowToastClose: true,
                    //             hideAfter: false
                    //         });
                    //     }
                    // }, 2000);
                },
                tryPassFail: function (code) {
                    try {
                        new Function(code)();
                        return true;
                    }
                    catch (err) {
                        return false;
                    }
                }
            }
        }
    }
}